package com.ems.utils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * @Auther: 伟伟
 * @Date: 2020/3/19
 * @Description: 描述
 * @Version: 1.0
 */
public class SendEmail {
    private String to; // 收件人电子邮箱
    private String title;
    private String content;

    public SendEmail() {

    }

    public SendEmail(String to, String title, String content) {
        this.to = to;
        this.title = title;
        this.content = content;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public boolean send(){
        String form = "2055475891@qq.com";

        //获取系统属性
        Properties properties = System.getProperties();

        //设置邮件服务器
        properties.setProperty("mail.smtp.host", "smtp.qq.com");
        properties.put("mail.smtp.auth", "true");

        //获取默认session对象
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("2151538441@qq.com","viwpnioljtiqdjib");
            }
        });

        try {

            //创建默认的MimeMessage对象
            MimeMessage message = new MimeMessage(session);

            //设置头部字段
            message.setFrom(new InternetAddress(form));
            message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
            message.setSubject(title);

            //设置消息体
            message.setText(content);

            //发送消息
            Transport.send(message);

        }catch (MessagingException mex){
            mex.printStackTrace();
            return false;
        }

        return true;
    }

}
